package com.genius.adminmanager.menu.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuService;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBUtils;
import com.genius.workflow.util.Pattern;
import com.genius.workflow.util.TimeDiff;


@Controller
public class MenuInsertController {
	/**
	 * 跳转到菜单添加页面
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/adminmanager/menu/toInsert")
	public String toInsert(){
		return "/views/adminmanager/menu/insert";
	}
	
	@PostMapping("/adminmanager/menu/insert")
	@ResponseBody
	public JSONObject addMenu(@RequestBody JSONObject json, HttpSession session){
		JSONObject response = new JSONObject();
    	response.put(ResponseJson.code, 0);
		String lastMenuId = json.getString(MenuJson.menuId);
		String lastPid = json.getString(MenuJson.parentId);;//上级菜单的父ID
		String name = json.getString(MenuJson.name);
		String type = json.getString(MenuJson.type);
		String url = json.getString(MenuJson.url);
		String canshu = json.getString(MenuJson.canshu);
		String field = json.getString(MenuJson.field);
		String place = json.getString(MenuJson.place);
		String icons = json.getString(MenuJson.icon);
		String createTime = TimeDiff.getNowTime(Pattern.yyyy_MM_dd_HH_mm_ss);
		String status = json.containsKey(MenuJson.status)?"1":"0";
		
		json.put(MenuJson.status, status);
		json.put(MenuJson.createTime, createTime);
		
		JSONObject userJson = (JSONObject)session.getAttribute(SessionJson.currentUser);
		String labelName = userJson.getString(UserJson.name);//获取当前创建者
		if(labelName==null){
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.status, ResponseJson.msg_Session_Failure);
    		return response;
		}
		try {
			if(lastMenuId.equals("-1")){
				//说明是根目录下,直接插入
				MenuUtils.addMenu(lastMenuId,lastPid,"", name, "1", url ,canshu, "-1", place, type, labelName, createTime, status,field);
			}else{
				boolean isSuccess = false;
				//根据菜单先查找层级相关信息
				String selectState = "select "+MenuJson.state+","+MenuJson.url+","+MenuJson.type+" from t_menu where "+MenuJson.menuId+"=?";
				JSONObject stateJsonG = DBUtils.select(selectState, new String[]{lastMenuId});
				int stateP = stateJsonG.getInteger(MenuJson.state);
				String nowState = stateP+1+"";
				String urlP = stateJsonG.getString(MenuJson.url);
				String typeP = stateJsonG.getString(MenuJson.type);
				switch(MenuJson.menuType.valueOf(type)){
					case button:
						//如果是新增按钮，上级菜单必须是菜单类型的才可以
						if(!typeP.equals(MenuJson.menuType.menu.toString())){
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "上级目录是菜单类型才可以添加按钮");
							return response;
						}
						isSuccess = true;
						break;
					case link:
						//如果新增
						if(!typeP.equals(MenuJson.menuType.catalog.toString())){
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "上级目录是目录类型才可以添加链接");
							return response;
						}
						if(urlP != null&&!StringUtils.equalsIgnoreCase(urlP, "")){
							//如果上级菜单已经有地址了,就不能添加菜单了，只能添加按钮
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "请将上级目录目录地址清空后再操作");
							return response;
						}
						isSuccess = true;
						break;
					case catalog:
						if(!typeP.equals(MenuJson.menuType.catalog.toString())){
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "上级目录是目录 类型才可以添加目录");
							return response;
						}
						if(urlP != null&&!StringUtils.equalsIgnoreCase(urlP, "")){
							//如果上级目录已经有地址了,就不能添加目录了
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "请将上级目录地址清空后再操作");
							return response;
						}
						isSuccess = true;
						break;
					case menu:
						String source = json.getString("source");
						if(!typeP.equals(MenuJson.menuType.catalog.toString()) && !source.equals("platform")){
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "上级目录是目录类型才可以添加菜单");
							return response;
						}
						if((urlP != null&&!StringUtils.equalsIgnoreCase(urlP, ""))&& !source.equals("platform")){
							//如果上级菜单已经有地址了,就不能添加菜单了，只能添加按钮
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "请将上级目录目录地址清空后再操作");
							return response;
						}
						if(source.equals("platform")){
							if(typeP.equals(MenuJson.menuType.menu.toString()))
								type = "oneToMore";
							MenuService.loadPlatformModel(lastMenuId,lastPid,json, nowState, labelName,type);
						}else{
							isSuccess = true;
						}
						break;
					case newWindow:
						if(!typeP.equals(MenuJson.menuType.catalog.toString())){
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "上级目录是目录类型才可以添加新窗体");
							return response;
						}
						if(urlP != null&&!StringUtils.equalsIgnoreCase(urlP, "")){
							//如果上级菜单已经有地址了,就不能添加菜单了，只能添加按钮
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "请将上级目录菜单地址清空后再操作");
							return response;
						}
						isSuccess = true;
						break;
					case window:
						if(!typeP.equals(MenuJson.menuType.catalog.toString())){
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "上级目录是目录类型才可以添加弹窗体");
							return response;
						}
						if(urlP != null&&!StringUtils.equalsIgnoreCase(urlP, "")){
							//如果上级菜单已经有地址了,就不能添加菜单了，只能添加按钮
							response.put(ResponseJson.status, ResponseJson.status_Failed);
							response.put(ResponseJson.msg, "请将上级目录菜单地址清空后再操作");
							return response;
						}
						isSuccess = true;
						break;
				}
				if(isSuccess){
					MenuUtils.addMenu(lastMenuId,lastPid,icons, name, nowState, url, canshu, lastMenuId, place, type, labelName, createTime, status,field);
				}
			}
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, "添加菜单成功");
		} catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, "添加菜单出错");
		}
		return response;
	}
}
